package com.lanou3g.dllo.baidumusic.main.playmusic;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.tools.OnRemoveListener;

import java.util.ArrayList;

/**
 * Created by dllo on 16/6/30.
 */
public class PopAdapter extends BaseAdapter{
    private ArrayList<PlaymusicBean> datas;
    private Context context;
    private OnRemoveListener onRemoveListener;

    public void setOnRemoveListener(OnRemoveListener onRemoveListener) {
        this.onRemoveListener = onRemoveListener;
    }

    public PopAdapter(Context context) {
        this.context = context;
    }

    public void setDatas(ArrayList<PlaymusicBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public Object getItem(int position) {

        return datas == null ? null : datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null ) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_playmusic,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.popSongsName.setText(datas.get(position).getSongsName());
        holder.popSingerName.setText(datas.get(position).getSinger());
        holder.popClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRemoveListener.onRemoveClick(position);
            }
        });

        return convertView;
    }

    class ViewHolder{
        TextView popSongsName,popSingerName;
        ImageView popClear;

        public ViewHolder(View view) {
            popSongsName = (TextView) view.findViewById(R.id.pop_songsname_tv);
            popSingerName = (TextView) view.findViewById(R.id.pop_singer_tv);
            popClear = (ImageView) view.findViewById(R.id.pop_clear_iv);

        }
    }
}
